﻿<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Sample.Models.Person>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    MVC API Action Filter Demo
</asp:Content>
<asp:Content ContentPlaceHolderID="HeadContent" runat="server">
    <script type="text/javascript">
        $(document).ready(function () {
            dp.SyntaxHighlighter.HighlightAll("code");

            var resetResult = function () {
                var text = "";

                switch ($("#contentTypeSelect").val()) {
                    case "text/json": text = JSON.stringify({
                        FirstName: "Nick",
                        LastName: "Riggs",
                        Age: 30
                    }); break;

                    case "text/xml": text = "<Person><FirstName>Nick</FirstName><LastName>Riggs</LastName><Age>30</Age></Person>"; break;
                }

                $("#request").val(text);
                $("#contentTypeSelect").change(resetResult);
            };

            $("#sendButton").click(function () {
                var contentType = $("#contentTypeSelect").val();
                var acceptType = $("#acceptTypeSelect").val();

                var code = $("#request").val().replace(/\"/g, "\\\"");

                var requestCode =
                    "$.ajax({\n" +
                        "   type: \"POST\",\n" +
                        "   contentType: \"" + contentType + "\",\n" +
                        "   dataType: \"" + acceptType + "\",\n" +
                        "   data: \"" + code + "\",\n" +
                        "   url: \"Home/Create\"\n" +
                    "});";

                var requestCode = $("<pre name=\"requestCode\" class=\"javascript\"></pre>")
                    .text(requestCode);

                var requestContainer = $("#requestCodeContainer").children().remove().end().hide().append(requestCode);
                dp.SyntaxHighlighter.HighlightAll("requestCode");

                requestContainer.slideDown();

                var responseContainr = $("#responseCodeContainer").children().remove().end().hide();

                $.ajax({
                    type: "POST",
                    contentType: contentType,
                    dataType: acceptType,
                    data: $("#request").val(),
                    url: "Home/Create",
                    success: function (result) {
                        var responseCode;

                        switch (acceptType) {
                            case "json": responseCode = $("<pre name=\"responseCode\" class=\"javascript\"></pre>")
                                    .text(JSON.stringify(result)); break;
                            case "xml":
                                if (navigator.appName == "Microsoft Internet Explorer") {
                                    responseCode = $("<pre name=\"responseCode\" class=\"xml\"></pre>")
                                        .text(result.xml);                                    
                                } else {
                                    responseCode = $("<pre name=\"responseCode\" class=\"xml\"></pre>")
                                        .text((new XMLSerializer()).serializeToString(result));
                                }
                                break;
                            case "html": responseCode = $("<pre name=\"responseCode\" class=\"xml\"></pre>")
                                    .text($(result).find("#main").html());
                        }

                        responseContainr.append(responseCode);
                        dp.SyntaxHighlighter.HighlightAll("responseCode");
                        responseContainr.slideDown();
                    }
                });
            });

            resetResult();
        });
    </script>    
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <p>
        Download from CodePlex: <a href="http://mvcapi.codeplex.com/">http://mvcapi.codeplex.com/</a>
    </p>
    <h2>Setup</h2>
    <p>
        Person is our sample view model:
    </p>
    <pre name="code" class="c-sharp">
public class Person
{
	public Guid Id { get; set; }
	public string FirstName { get; set; }
	public string LastName { get; set; }
	public int Age { get; set; }
}
    </pre>
    <p>
        We decorate our controller's existing Create action with the [Api] attribute:
    </p>
    <pre name="code" class="c-sharp">
public class HomeController : Controller
{
	[Api]
	public ActionResult Create(Person person)
	{
		person.Id = Guid.NewGuid();
		_people.Add(person);

		return View(person);
	}
}    
    </pre>
    <h2>Let's Play</h2>
    <div class="field">
        Content Type:
        <select id="contentTypeSelect">
            <option value="text/json">Json</option>
            <option value="text/xml">Xml</option>
        </select>
    </div>
    <div class="field">
        Accept Type:
        <select id="acceptTypeSelect">
            <option value="json">Json</option>
            <option value="xml">Xml</option>
            <option value="html">Html</option>
        </select>
    </div>
    <div style="clear: both;">
        <div>Request:</div>
        <textarea id="request" rows="5" cols="100">
        </textarea>
    </div>
    <div>        
        <input id="sendButton" type="button" value="Send API Request" />
    </div>
    <h3>Request:</h3>
    <div id="requestCodeContainer">
    </div>
    <h3>Response:</h3>
    <div id="responseCodeContainer">
    </div>
</asp:Content>
